const pathRes = require('path').resolve
const cp = require('child_process')
const PlatformOS = require('os')

const pk = {}
module.exports = pk

pk.platform = {
  ios: 'ios',
  android: 'android'
}

pk.env = {
  debug: 'debug',
  release: 'release'
}

pk.run = (platform, env) => {
  platform = platform || pk.platform.android
  env = env || pk.env.debug

  let name = ''
  if (platform === pk.platform.android) {
    name = 'android'
  } else {
    name = 'ios'
  }

  if (env === pk.env.debug) {
    name += 'Debug'
  } else {
    name += 'Release'
  }
  name += '.sh'
  shellRun(name)
}

function shellRun(name) {
  const filePath = pathRes(__dirname, '..', 'sh', name)
  console.log('filePath=', filePath)
  let e = null
  // {stdio:'inherit'} -- 子进程将使用父进程的标准输入输出

  const platformType = PlatformOS.type()
  console.log('os.type=', platformType)
  if (platformType === 'Windows_NT') {
    // win
    e = cp.spawn('sh', [filePath], {stdio:'inherit'})
  } else {
    // Mac / Linux
    e = cp.spawn(filePath, {stdio:'inherit'})
  }

  e.on('exit', (code)=> {
    console.log('child process exited with code: '+ code)
  })
}
